package com.jeesite.modules.rt.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.rt.entity.RtDeviceHardware;
import com.jeesite.modules.rt.service.RtDeviceHardwareService;

/**
 * 硬件信息Controller
 * @author 李寒冰
 * @version 2024-09-26
 */
@Controller
@RequestMapping(value = "${adminPath}/rt/rtDeviceHardware")
public class RtDeviceHardwareController extends BaseController {

	@Autowired
	private RtDeviceHardwareService rtDeviceHardwareService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public RtDeviceHardware get(String id, boolean isNewRecord) {
		return rtDeviceHardwareService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("rt:rtDevice:view")
	@RequestMapping(value = {"list", ""})
	public String list(RtDeviceHardware rtDeviceHardware, Model model) {
		model.addAttribute("rtDeviceHardware", rtDeviceHardware);
		return "modules/rt/rtDeviceHardwareList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("rt:rtDevice:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<RtDeviceHardware> listData(RtDeviceHardware rtDeviceHardware, HttpServletRequest request, HttpServletResponse response) {
		rtDeviceHardware.setPage(new Page<>(request, response));
		Page<RtDeviceHardware> page = rtDeviceHardwareService.findPage(rtDeviceHardware);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("rt:rtDevice:view")
	@RequestMapping(value = "form")
	public String form(RtDeviceHardware rtDeviceHardware, Model model) {
		String sn = rtDeviceHardware.getSn();
		RtDeviceHardware oneBySn = rtDeviceHardwareService.findOneBySn(sn);
		if(oneBySn==null){
			rtDeviceHardware = new RtDeviceHardware();
			rtDeviceHardware.setSn(sn);
		}else {
			rtDeviceHardware = oneBySn;
			rtDeviceHardware.setTotal(rtDeviceHardware.getTotal() + "G");
			rtDeviceHardware.setUsed(rtDeviceHardware.getUsed() + "G");
			rtDeviceHardware.setXtUsed(rtDeviceHardware.getXtUsed() + "G");
			rtDeviceHardware.setXtTotal(rtDeviceHardware.getXtTotal() + "G");
			rtDeviceHardware.setMemTotal(rtDeviceHardware.getMemTotal()+"G");
			rtDeviceHardware.setCpuCore(rtDeviceHardware.getCpuCore()+"核");
		}
		model.addAttribute("rtDeviceHardware", rtDeviceHardware);
		return "modules/rt/rtDeviceHardwareForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("rt:rtDevice:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated RtDeviceHardware rtDeviceHardware) {
		rtDeviceHardwareService.save(rtDeviceHardware);
		return renderResult(Global.TRUE, text("保存硬件信息成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("rt:rtDevice:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(RtDeviceHardware rtDeviceHardware) {
		rtDeviceHardwareService.delete(rtDeviceHardware);
		return renderResult(Global.TRUE, text("删除硬件信息成功！"));
	}
	
}